package com.captcha.controller;

import com.captcha.model.CheckImageData;
import com.captcha.model.ResponseData;
import com.captcha.model.SliderCaptchaImg;
import com.captcha.util.VerifyImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import static com.captcha.util.CreatImageUtil.createImage;

/**
 * @program: captchaTest
 * @description:
 * @author: Wanglipeng
 * @create: 2022-03-07 17:53
 **/
@Controller
public class CaptchController {

    @GetMapping("/index")
    public String index() {
        return "index1";
    }

    ConcurrentHashMap<String,Integer> concurrentHashMap = new ConcurrentHashMap();

    //获取原始图片，抠图，抠图后的图片

    @RequestMapping("createImgValidate")
    @ResponseBody
    public ResponseData createImgValidate(){
        try {
            //随机一个图片进行
            Integer templateNum = new Random().nextInt(8)+1;
           /* Integer targetNum = new Random().nextInt(20) + 1;*/
            /*File templateFile = ResourceUtils.getFile("classpath:static/images/validate/template/"+templateNum+".png");
            File targetFile = ResourceUtils.getFile("classpath:static/images/validate/target/"+targetNum+".jpg");*/
           /* File templateFile = ResourceUtils.getFile("classpath:bgimages/target.jpg");*/
            File targetFile = ResourceUtils.getFile("classpath:bgimages/"+templateNum+".jpg");
            /*Map<String, byte[]> pictureMap = VerifyImageUtil.pictureTemplatesCut(templateFile, targetFile,
                    "jpg","jpg");*/
            // 将生成的偏移位置信息设置到redis中
            /*String key = ConstString.WEB_VALID_IMAGE_PREFIX + vo.getTelephone();
            boolean verified = redisUtil.exists(key);
            if(verified){
                redisUtil.del(key);
            }*/
            Map<String, Object> pictureMap = createImage(targetFile);
            //将偏移量存放redis中（目前是测试，存放map中）
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            System.out.println("uuid="+uuid);
            concurrentHashMap.put("image"+uuid,(Integer) pictureMap.get("xWidth")+67);
            SliderCaptchaImg sliderCaptchaImg = new SliderCaptchaImg();
            sliderCaptchaImg.setNewImage((byte[]) pictureMap.get("newImage"));
            sliderCaptchaImg.setOriCopyImage((byte[]) pictureMap.get("oriCopyImage"));
            sliderCaptchaImg.setHeightRandom((Integer) pictureMap.get("heightRandom"));
            sliderCaptchaImg.setImageId(uuid);
           /* redisUtil.set(key,(VerifyImageUtil.getX()+67)+"",SmsUtil.VALID_IMAGE_TIMEOUT);*/
            System.out.println("sliderCaptchaImg="+sliderCaptchaImg);
            return ResponseData.success(sliderCaptchaImg);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error("系统异常");
        }
    }


    //校验图片
    @RequestMapping(value = "checkImgValidate",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ResponseData checkImgValidate(@RequestParam("checkImgValidate") Integer xheight,@RequestParam("imageId") String uuid){
        Integer Xheight = xheight;
        String  imageId = uuid;
        System.out.println("Xheight="+Xheight+",imageId="+imageId);
        Integer getXH = concurrentHashMap.get("image"+imageId);
        System.out.println("getXH="+getXH);
        Integer a = getXH - Xheight;
        System.out.println("a="+a);
        if(a<10 && a>-10){
            //允许有前后10px的差距
            return ResponseData.success();
        }

        return ResponseData.error("请重新滑动");
    }

}
